package cn.dankal.flyaudio.activity.search.searchfragment;

import android.annotation.SuppressLint;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.dankal.basiclib.base.callback.DKCallBackBoolean;
import cn.dankal.basiclib.base.fragment.BaseFragment;
import cn.dankal.basiclib.util.FileUtils;
import cn.dankal.basiclib.widget.CommonDialog;
import cn.dankal.business.pojo.BaseBeanList;
import cn.dankal.business.pojo.HotSearchBean;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.activity.search.adapter.HotSearchAdapter;
import cn.dankal.flyaudio.activity.search.adapter.SearchHistoryAdapter;

/**
 * @author ricky
 * @date 2018/8/14 0014
 */
@SuppressLint("ValidFragment")
public class SearchFragment extends BaseFragment implements SearchFContract.SearchFView {
    @BindView(R.id.rv_hot_search)
    RecyclerView rvHotSearch;
    @BindView(R.id.rv_search_history)
    RecyclerView rvSearchHistory;
    @BindView(R.id.iv_detele_history)
    ImageView ivDeteleHistory;
    @BindView(R.id.bt_detele_history)
    Button btDeteleHistory;

    @BindView(R.id.tv_hot_title)
    TextView tvHotTitle;

    CommonDialog commonDialog;

    private List<HotSearchBean> hotGoodsList = new ArrayList();
    private FlexboxLayoutManager flexboxLayoutManager;
    private View.OnClickListener onClickListener;
    private SearchFPresenter searchFPresenter;

    @SuppressLint("ValidFragment")
    public SearchFragment(View.OnClickListener onClickListener) {

        this.onClickListener = onClickListener;
    }


    public void updateSearchHisgtoryList() {

        this.searchHisgtoryList.clear();
        FileUtils.readFileToList(getContext(), "search_history", this.searchHisgtoryList);
        rvSearchHistory.getAdapter().notifyDataSetChanged();

    }

    private List<String> searchHisgtoryList = new ArrayList<>(1);

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_search;
    }

    @Override
    protected void initComponents() {

        //搜索历史
        RecyclerView.LayoutManager rvSearchHistoryManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        rvSearchHistory.setLayoutManager(rvSearchHistoryManager);
        //读取本地保存历史搜索记录
        FileUtils.readFileToList(getContext(), "search_history", searchHisgtoryList);
        rvSearchHistory.setAdapter(new SearchHistoryAdapter(getContext(), searchHisgtoryList, onClickListener));
        //灵活布局设置
        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(getActivity());
        layoutManager.setFlexWrap(FlexWrap.WRAP);
        layoutManager.setFlexDirection(FlexDirection.ROW);
        layoutManager.setAlignItems(AlignItems.STRETCH);
        layoutManager.setJustifyContent(JustifyContent.FLEX_START);
        rvHotSearch.setLayoutManager(layoutManager);
        rvHotSearch.setOverScrollMode(View.OVER_SCROLL_NEVER);
        rvHotSearch.setAdapter(new HotSearchAdapter(getContext(), hotGoodsList, onClickListener));
        searchFPresenter=new SearchFPresenter();
        searchFPresenter.attachView(this);
    }

    @Override
    public void obtainData() {
        super.obtainData();
        searchFPresenter.obtainHotSearch();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (searchFPresenter != null) {
            searchFPresenter.detachView();
        }
    }
    /**
     * 获取热门搜索列表成功
     *
     * @param hotGoodsResponse
     */
    @Override
    public void obtainHotSearchSuccess(BaseBeanList<HotSearchBean> hotGoodsResponse) {
        if (hotGoodsResponse != null&&hotGoodsResponse.list!=null) {
            if (hotGoodsResponse.list.isEmpty()){
                tvHotTitle.setVisibility(View.GONE);
            }else {
                tvHotTitle.setVisibility(View.VISIBLE);
                hotGoodsList.clear();
                //移除各种因素导致的空热门搜索词
                for (int i = 0; i < hotGoodsResponse.list.size(); i++) {
                    if (TextUtils.isEmpty(hotGoodsResponse.list.get(i).getTitle()))
                        hotGoodsResponse.list.remove(i);
                }
                hotGoodsList.addAll(hotGoodsResponse.list);
                rvHotSearch.getAdapter().notifyDataSetChanged();
            }


        }
    }


    /**
     * 清除搜索历史记录点击事件
     */
    @OnClick(R.id.bt_detele_history)
    public void onViewClicked() {

        CommonDialog.Builder builder = new CommonDialog.Builder(getContext());
        builder.setTitle("确定删除全部历史记录");
        builder.setNegativeButton("取消", getResources().getColor(R.color.color00), new DKCallBackBoolean() {
            @Override
            public void action(int type) {
                commonDialog.dismiss();
            }
        });
        builder.setPositiveButton("确认", getResources().getColor(R.color.color00), new DKCallBackBoolean() {
            @Override
            public void action(int type) {
                FileUtils.clearFile(getContext(), "search_history");
                updateSearchHisgtoryList();
                commonDialog.dismiss();
            }
        });
        commonDialog = builder.create();
        commonDialog.show();
    }


}
